package com.profgj.profgj.controller;

import com.profgj.profgj.entity.Role;
import com.profgj.profgj.enums.ServerStatus;
import com.profgj.profgj.service.RoleService;
import com.profgj.profgj.utils.EmptyUtils;
import com.profgj.profgj.utils.ReturnOperator;
import com.profgj.profgj.vo.EmptyObject;
import com.profgj.profgj.vo.ResultMsg;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (用户角色管理)表控制层
 *
 * @author 杨帅
 * @since 2021-06-22 11:09:35
 */

/**
 * JSON格式返回
 */
@RestController
/**
 * 请求基路径
 */
@RequestMapping("/role")
@Api(tags = {"用户角色管理"},description = "用户角色管理")
public class RoleController {

    /**
     * 用户角色服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 返回空对象的替代品
     */
    private EmptyObject emptyObject = new EmptyObject();


    /**
     * id查询用户组角色
     * @param id 角色id
     * @return
     */
    @GetMapping("/role/{id}")
    @ApiOperation(value = "id查询用户组角色",produces = "application/json",consumes = "application/json")
    @ApiImplicitParam(name = "id",value = "角色id", required = true, dataType = "Integer", paramType = "Integer", dataTypeClass = Integer.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "表明该请求被成功地完成,所请求的资源发送到客户端"),
            @ApiResponse(code = 401, message = "请求要求身份验证,常见于需要登录而用户未登录的情况"),
            @ApiResponse(code = 403, message = "服务器拒绝请求,常见于机密信息或复制其它登录而用户未登录的情况"),
            @ApiResponse(code = 404, message = "服务器无法取得所请求的网页,请求资源不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public ResponseEntity<ResultMsg> groupQueryByIdMethod(@PathVariable("id") Integer id){

        // 参数校验
        if (!EmptyUtils.isIntegerEmpty(id)) {
            return ReturnOperator.createResultMsg("", ServerStatus.ID_NOT_NULL, ServerStatus.ERROR,
                    new ArrayList<>(), emptyObject, HttpStatus.OK);
        }

        // id查询角色信息
        Role role = roleService.queryById(id);

        // 查询结果校验
        if (!EmptyUtils.isRoleEmpty(role)) {
            return ReturnOperator.createResultMsg("", ServerStatus.ROLE_NOT_EXISTS, ServerStatus.ERROR,
                    new ArrayList<>(), emptyObject, HttpStatus.OK);
        }

        // 返回数据
        return ReturnOperator.createResultMsg("", ServerStatus.QUERY_MESSAGE_SUCCESS, ServerStatus.OK,
                new ArrayList<>(), role, HttpStatus.OK);
    }

    /**
     * 查询用户组角色列表
     * @return
     */
    @GetMapping("/role/list")
    @ApiOperation(value = "查询用户组角色列表",produces = "application/json",consumes = "application/json")
    @ApiResponses({
            @ApiResponse(code = 200, message = "表明该请求被成功地完成,所请求的资源发送到客户端"),
            @ApiResponse(code = 401, message = "请求要求身份验证,常见于需要登录而用户未登录的情况"),
            @ApiResponse(code = 403, message = "服务器拒绝请求,常见于机密信息或复制其它登录而用户未登录的情况"),
            @ApiResponse(code = 404, message = "服务器无法取得所请求的网页,请求资源不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public ResponseEntity<ResultMsg> groupQueryListMethod(){

        // 查询所有用户角色
        List<Role> roles = roleService.queryAll2();

        if (!EmptyUtils.isRoleListEmpty(roles)) {
            return ReturnOperator.createResultMsg("", ServerStatus.ROLE_NOT_EXISTS, ServerStatus.ERROR,
                    new ArrayList<>(), emptyObject, HttpStatus.OK);
        }

        // 返回数据
        return ReturnOperator.createResultMsg("", ServerStatus.QUERY_MESSAGE_SUCCESS, ServerStatus.OK,
                roles, emptyObject, HttpStatus.OK);
    }
}
